from xml.dom.minidom import parseString,parse
from xml.dom import Node
from obj_types.obj_image import Image
from obj_types.obj_text import Text

class classparser:
    __doc__=""" 
A class to hold class data - you shoudn't need more than one
    
Fields:
    students - a lsit containing all the students
           
Methods:
    __init__(class, is_file) - constructor, accepts 1 paramater
    has_student(student) - returns True/False
    get_students() - returns a list with all student names
""" 

    def __init__(self,classfile,isfile):
        #presentation - string containing xml data
        self.students=[]
        self.is_ok=True
        
    #    try:
        if isfile:
            try:
                document=parse(classfile)
            except IOError:
                self.is_ok=False
                return
        else:
            document=parseString(classfile)

        #add all the students in the list
        for slide in document.getElementsByTagName('class').item(0).childNodes:
           if slide.nodeType==Node.ELEMENT_NODE and slide.nodeName=='student':
               if slide.hasAttribute('name'):
                   #student name as attribute
                   self.students.append(slide.getAttributeNode('name').nodeValue)
               elif slide.firstChild.nodeType == Node.TEXT_NODE:
                   #student name as child
                   self.students.append(slide.firstChild.nodeValue)
        del document
     #   except:
     #       self.is_ok=False
    
    def get_students(self):
        return self.students
    
    def is_in_class(self,student):
        return (student in self.students)